//
//  TFConfigTool.m
//  TFBase
//
//  Created by Liyadong on 2019/6/12.
//  Copyright © 2019 Liyadong. All rights reserved.
//

#import "TFConfigTool.h"

@implementation TFConfigTool

static TFConfigTool *singleton = nil;
+ (instancetype)shareInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        singleton = [[self alloc] init];
    });
    return singleton;
}

/** 获取app版本 */
+ (NSString *)tfAppVersion {
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    return  [infoDictionary objectForKey:@"CFBundleShortVersionString"];
}
 
+ (NSString*)loadTimestamp {
    
    NSTimeInterval  timeInterval = [[NSDate date] timeIntervalSince1970];
    
    double timestamp = timeInterval * 1000;
    
    long long str = [[NSNumber numberWithDouble:timestamp] longLongValue];
    
    return [NSString stringWithFormat:@"%lld",str];
    
}
  

/** 通过行数, 返回将来时间 */
+ (NSString *)compareForFutureCurrentTime:(NSString *)timestamp
{
    // 创建日历对象
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    // 获取当前时间
    NSDate *currentDate = [NSDate date];
    
    // 获取当前时间的年、月、日。利用日历
    NSDateComponents *components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
    NSInteger currentYear = components.year;
    NSInteger currentMonth = components.month;
    NSInteger currentDay = components.day;
    
    // 获取消息发送时间的年、月、日
    NSDate *msgDate = [NSDate dateWithTimeIntervalSince1970:[timestamp doubleValue]];
    components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay fromDate:msgDate];
    CGFloat msgYear = components.year;
    CGFloat msgMonth = components.month;
    CGFloat msgDay = components.day;
    
    // 进行判断
    NSDateFormatter *dateFmt = [[NSDateFormatter alloc] init];
    if (currentYear == msgYear && currentMonth == msgMonth && currentDay == msgDay) {
        //今天
        dateFmt.dateFormat = @"今天 HH:mm";
    } else if (currentYear == msgYear && currentMonth == msgMonth && currentDay + 1 == msgDay ){
        //明天
        dateFmt.dateFormat = @"明天 HH:mm";
    } else if (currentYear == msgYear && currentMonth == msgMonth && currentDay + 2 == msgDay ){
        //后天
        dateFmt.dateFormat = @"后天 HH:mm";
    } else {
        //前天以前
        dateFmt.dateFormat = @"yyyy年MM月dd日 HH:mm";
    }
    // 返回处理后的结果
    return [dateFmt stringFromDate:msgDate];
}

/** 通过时间戳, 返回将来 今天 12月18日 18:45  */
+ (NSString *)compareForFutureDayDateTimeCurrentTime:(NSString *)timestamp
{
    // 创建日历对象
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    // 获取当前时间
    NSDate *currentDate = [NSDate date];
    
    // 获取当前时间的年、月、日。利用日历
    NSDateComponents *components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
    NSInteger currentYear = components.year;
    NSInteger currentMonth = components.month;
    NSInteger currentDay = components.day;
    
    // 获取消息发送时间的年、月、日
    NSDate *msgDate = [NSDate dateWithTimeIntervalSince1970:[timestamp doubleValue]];
    components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay fromDate:msgDate];
    CGFloat msgYear = components.year;
    CGFloat msgMonth = components.month;
    CGFloat msgDay = components.day;
    
    // 进行判断
    NSDateFormatter *dateFmt = [[NSDateFormatter alloc] init];
    if (currentYear == msgYear && currentMonth == msgMonth && currentDay == msgDay) {
        //今天
        dateFmt.dateFormat = @"MM月dd日 HH:mm";
        return [NSString stringWithFormat:@"今天%@",[dateFmt stringFromDate:msgDate]];

    } else if (currentYear == msgYear && currentMonth == msgMonth && currentDay + 1 == msgDay ){
        //明天
        dateFmt.dateFormat = @"MM月dd日 HH:mm";
        return [NSString stringWithFormat:@"明天%@",[dateFmt stringFromDate:msgDate]];

    } else if (currentYear == msgYear && currentMonth == msgMonth && currentDay + 2 == msgDay ){
        //后天
        dateFmt.dateFormat = @"MM月dd日 HH:mm";
        return [NSString stringWithFormat:@"后天%@",[dateFmt stringFromDate:msgDate]];

    } else {
        //前天以前
        dateFmt.dateFormat = @"yyyy年MM月dd日 HH:mm";
    }
    // 返回处理后的结果
    return [dateFmt stringFromDate:msgDate];
}


/** 通过行数, 返回过去日期 */
+ (NSString *)compareForDateCurrentTime:(NSString *)timestamp
{
    // 创建日历对象
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    // 获取当前时间
    NSDate *currentDate = [NSDate date];
    
    // 获取当前时间的年、月、日。利用日历
    NSDateComponents *components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
//    NSInteger currentYear = components.year;
//    NSInteger currentMonth = components.month;
//    NSInteger currentDay = components.day;
    
    // 获取消息发送时间的年、月、日
    NSDate *msgDate = [NSDate dateWithTimeIntervalSince1970:[timestamp doubleValue]];
    components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay fromDate:msgDate];
//    CGFloat msgYear = components.year;
//    CGFloat msgMonth = components.month;
//    CGFloat msgDay = components.day;
    
    // 进行判断
    NSDateFormatter *dateFmt = [[NSDateFormatter alloc] init];
    dateFmt.dateFormat = @"yyyy.MM.dd";
    // 返回处理后的结果
    return [dateFmt stringFromDate:msgDate];
}

/** 通过行数, 返回时间 */
+ (NSString *)compareForTimeCurrentTime:(NSString *)timestamp
{
    // 创建日历对象
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    // 获取当前时间
    NSDate *currentDate = [NSDate date];
    
    // 获取当前时间的年、月、日。利用日历
    NSDateComponents *components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
//    NSInteger currentYear = components.year;
//    NSInteger currentMonth = components.month;
//    NSInteger currentDay = components.day;

    // 获取消息发送时间的年、月、日
    NSDate *msgDate = [NSDate dateWithTimeIntervalSince1970:[timestamp doubleValue]];
    components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay fromDate:msgDate];
//    CGFloat msgYear = components.year;
//    CGFloat msgMonth = components.month;
//    CGFloat msgDay = components.day;
    
    // 进行判断
    NSDateFormatter *dateFmt = [[NSDateFormatter alloc] init];
    dateFmt.dateFormat = @"HH:mm:ss";
    // 返回处理后的结果
    return [dateFmt stringFromDate:msgDate];
}

/** 通过行数, 返回过去时间 */
+ (NSString *)compareCurrentTime:(NSString *)timestamp
{
    // 创建日历对象
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    // 获取当前时间
    NSDate *currentDate = [NSDate date];
    
    // 获取当前时间的年、月、日。利用日历
    NSDateComponents *components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
    NSInteger currentYear = components.year;
    NSInteger currentMonth = components.month;
    NSInteger currentDay = components.day;
    
    // 获取消息发送时间的年、月、日
    NSDate *msgDate = [NSDate dateWithTimeIntervalSince1970:[timestamp doubleValue]];
    components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay fromDate:msgDate];
    CGFloat msgYear = components.year;
    CGFloat msgMonth = components.month;
    CGFloat msgDay = components.day;
    
    // 进行判断
    NSDateFormatter *dateFmt = [[NSDateFormatter alloc] init];
    if (currentYear == msgYear && currentMonth == msgMonth && currentDay == msgDay) {
        //今天
        dateFmt.dateFormat = @"今天 HH:mm";
    } else if (currentYear == msgYear && currentMonth == msgMonth && currentDay-1 == msgDay ){
        //昨天
        dateFmt.dateFormat = @"昨天 HH:mm";
    } else if (currentYear == msgYear && currentMonth == msgMonth && currentDay-2 == msgDay ){
        //昨天
        dateFmt.dateFormat = @"前天 HH:mm";
    } else {
        //前天以前
        dateFmt.dateFormat = @"yyyy年MM月dd日 HH:mm";
    }
    // 返回处理后的结果
    return [dateFmt stringFromDate:msgDate];
}

+ (NSString *)turnToDataStrWithTimes:(NSInteger)times
{
    NSString *timestamp = [@(times) stringValue];
    if (timestamp.length == 13) {
        timestamp = [NSString stringWithFormat:@"%@", @([timestamp integerValue] / 1000)];
    }
    
    // 创建日历对象
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    // 获取当前时间
    NSDate *currentDate = [NSDate date];
    
    // 获取当前时间的年、月、日。利用日历
    NSDateComponents *components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
 
    // 获取消息发送时间的年、月、日
    NSDate *msgDate = [NSDate dateWithTimeIntervalSince1970:[timestamp doubleValue]];
    components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay fromDate:msgDate];
 
    // 进行判断
    NSDateFormatter *dateFmt = [[NSDateFormatter alloc] init];
    
    dateFmt.dateFormat = @"yyyyMMdd";
    // 返回处理后的结果
    return [dateFmt stringFromDate:msgDate];
}


+ (NSInteger)timeSwitchTimestamp:(NSString *)formatTime {
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    
    [formatter setTimeZone:timeZone];
    NSDate* date = [formatter dateFromString:formatTime]; //------------将字符串按formatter转成nsdate
    //时间转时间戳的方法:
    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
    
    return timeSp;
}


@end
